#region Using Statements

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

#endregion

namespace _1942
{
    /// <summary>
    /// This game component implements a manager for all Kamikaze planes in the game.
    /// </summary>
    public class ProjectileManager : DrawableGameComponent
    {

        #region Variables 

        // List of active planes
        protected List<Projectile> projectiles;
        // Audio
        private AudioLibrary audio;

        protected Texture2D projectileTexture;
        protected TimeSpan elapsedTime = TimeSpan.Zero;

        //What source, player p, kamikaze k
        private ProjectileType source;

        #endregion

        #region Constructors

        public ProjectileManager(Game game, ref Texture2D theTexture, ProjectileType source) : base(game)
        {
            projectileTexture = theTexture;
            projectiles = new List<Projectile>();

            // Get the audio library
            audio = (AudioLibrary)Game.Services.GetService(typeof(AudioLibrary));
            
            this.source = source;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Allows the game component to perform any initialization it needs to 
        /// before starting to run.  This is where it can query for any required
        /// services and load content.
        /// </summary>
        public override void Initialize()
        {
            projectiles.Clear();
            base.Initialize();
        }

        public void Reset()
        {
            Initialize();
        }


        protected override void UnloadContent()
        {
            this.projectileTexture.Dispose();

            base.UnloadContent();
        }

        /// <summary>
        /// Add a projectile
        /// </summary>
        public Projectile AddNewProjectile(Vector2 origin, Vector2 velocity, ProjectileType source)
        {
            Projectile newProj = new Projectile(Game, ref projectileTexture, origin, velocity, source);
            
            newProj.Initialize();
            
            projectiles.Add(newProj);

            return newProj;
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // Update Projectiles
            foreach (Projectile item in projectiles)
            {
                if ( item.Enabled )
                    item.Update(gameTime);
            }

            // Delete all "old" projectiles.
            projectiles.RemoveAll(   delegate(Projectile item)
            {
                if (!item.Enabled)
                    return true;
                return false;
            }
            );

            base.Update(gameTime);
        }

        /// <summary>
        /// Allows the game component draw your content in game screen
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            // Draw the projectiles
            foreach (Projectile item in projectiles)
            {
                if ( item.Visible )
                    item.Draw(gameTime);
            }

            base.Draw(gameTime);
        }

        #endregion

        #region Properties

        /// <summary>
        /// All Projectiles in the game
        /// </summary>
        public List<Projectile> AllProjectiles
        {
            get { return projectiles; }
        }

        #endregion

    }
}